local nl = require'luaNumeric'
require 'luaFDTD3D'
local plot = require "Q2DViewerEx"

----------------------------------------------------------------------------------------------------------------------------------------
--初始化参数
local BlochPost={
	funs = {};
	xAxis = 0;
	ytype = 2;
	xtype = 1;
};
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function BlochPost:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	o:init();
	return o;
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function BlochPost:init ()
	self.funs = {};
	self.funs.abs = function(input) return nl.abs(input); end;
	self.funs.phs = function(input) return nl.arg(input)*180/pi; end;
	self.funs.real = function(input) return nl.real(input); end;
	self.funs.imag = function(input) return nl.imag(input); end;
	self.funs.log = function(input) 
		local  d = nl.abs(input);
		d = 20* nl.log10(d)
		return d; 
	end;
	self.funs.log1 = function(input) 
		local  d = nl.abs(input);
		d = 10* nl.log10(d)
		return d; 
	end;
	self.fun_names = {};
	self.fun_names[1] = 'log';
	self.fun_names[2] = 'abs';
	self.fun_names[3] = 'phs';
	self.fun_names[4] = 'real';
	self.fun_names[5] = 'imag';	
	self.fun_names[6] = 'log1';	
	self.ytype = 2;
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--更新数据
function BlochPost:refresh_figs ()
	self.probe:update();
	self.results = self.probe:results();
	self:plot()
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--画图
function BlochPost:plot ()
	self.view:clearGraphs();
	local x = self.results.freq;
	if self.xtype ==2 then
		x = self.results.lambda;
	end
	local fun = self.funs[ self.fun_names[self.ytype] ];
	--assert( self.results.data['Sout'], '数据不存在！' );
	--self.view:addGraph(x,  fun( self.results.data['Sout'] ), 'S11' );
	--self.view:rescaleAxes();
	--self:set_axis();
	for k,v in pairs(self.results.data) do
		self.view:addGraph(x,  fun( v ), k );
		self.view:rescaleAxes();	
	end
	self:set_axis();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--画图
function BlochPost:rescale ()
	self:update_parameters_from_ui();
	self:plot();
end
----------------------------------------------------------------------------------------------------------------------------------------

function BlochPost:set_axis()
	--print(self.xtype,self.ytype);
	if self.xtype ==2 then
		self.view.xAxis:setProperty( {name="波长 ["..Geometry.ref_units_dim_str()..']'} )
	else
		self.view.xAxis:setProperty( {name="频率 ["..Geometry.ref_units_freq_str()..']'} )
	end
	if self.ytype ==1 or self.ytype==6 then
		self.view.yAxis:setProperty( {name="Fields (dB)"} )
	elseif self.ytype ==2 then
		self.view.yAxis:setProperty( {name="Amplitude"} )
	elseif self.ytype ==3 then
		self.view.yAxis:setProperty( {name="Phase (Deg.)"} )
	elseif self.ytype ==4 then
		self.view.yAxis:setProperty( {name="Real part of fields"} )
	elseif self.ytype ==5 then
		self.view.yAxis:setProperty( {name="Image part of fields"} )
	end
	 self.view:rescaleAxes();
end

----------------------------------------------------------------------------------------------------------------------------------------
--保存数据
function BlochPost:export_data (path)
	print(path)
	--self.probe:update();
	self.probe:save(path);
end
----------------------------------------------------------------------------------------------------------------------------------------

return BlochPost;
